package com.example.e_booker;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.example.myfirstapp.R;

public class DisplayMessageActivity extends Activity implements Runnable{
	
	private Socket sockCliente;
	private final int puerto = 4444;
	private final String direccion = "201.229.227.205";
    PrintWriter out = null;
    BufferedReader in = null;
    String mostrar;
    Thread hilo;
    String message;
    String[] split;
	
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_display_message);
		// Show the Up button in the action bar.
		setupActionBar();
	    // Get the message from the intent
	    Intent intent = getIntent();
	    message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
	    //String[] message = intent.getStringArrayExtra(MainActivity.EXTRA_MESSAGE);
 
	    
	    hilo = new Thread(this);    
	    hilo.start();
	    
	    while(hilo.isAlive()){
	    // Create the text view
	    TextView textView = new TextView(this);
	    textView.setTextSize(40);
	    textView.setText(mostrar);

	    

	    // Set the text view as the activity layout
	    setContentView(textView);
	    }

	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.display_message, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		
	    split = message.split(":");
		InetAddress serverAddr;
		try {
			serverAddr = InetAddress.getByName(direccion);
			System.out.println("Conectando con Servidor...");
			
			sockCliente = new Socket(serverAddr,puerto);
		    out = new PrintWriter(sockCliente.getOutputStream(), true);
		    in = new BufferedReader(new InputStreamReader(sockCliente.getInputStream()));
		    String fromserver;
		    String[] serverSplit;
		    
		    while((fromserver = in.readLine())!=null){
		    	if( ( serverSplit = fromserver.split(":") )!=null){
		    		if(serverSplit[0].equals("msg")){
		    			mostrar = serverSplit[1];
		    			out.println("close");
		    			break;
		    		}
		    	
		    	}
		    	System.out.println(fromserver);
		    	out.println("login:"+split[0]+":"+split[1]);
		    	
	    	
		    }
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(e.toString());
			System.out.println("Causa: "+e.getCause());
			out.close();
			try {
				sockCliente.close();
				in.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(e.toString());
			System.out.println("Causa: "+e.getCause());
			
	
		}
	

		
	}

}
